package com.gxuwz.smem.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.smem.beans.Menu;
import com.gxuwz.smem.beans.Permission;
import com.gxuwz.smem.beans.Role;
import com.gxuwz.smem.dao.MenuQueryDao;
import com.gxuwz.smem.utils.JdbcUtils;



public class MenuQueryDaoImpl implements MenuQueryDao {

	@Override
	public Menu find(int role_id) {
		// TODO Auto-generated method stub
		List<Menu> list =new ArrayList<Menu>();
		 Connection conn = JdbcUtils.getConnection();
		    PreparedStatement stmt = null;
		    	ResultSet rs = null;

		    	Menu menu=new Menu();
	      try {
				
				stmt = conn.prepareStatement("select * from role where role_id='"+role_id+"'"); 		
				rs = stmt.executeQuery();
				if (rs.next()) { 	 
					menu=new Menu(rs.getInt("role_id"),rs.getString("real_name"),rs.getString("role_name"));
					list.add(menu);
				}else {
					menu=null;
				}
		
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JdbcUtils.close(conn, stmt, rs);
			}
		return null;
	}

	@Override
	public Menu find1(String real_name) {
		// TODO Auto-generated method stub
		List<Menu> list =new ArrayList<Menu>();
		 Connection conn = JdbcUtils.getConnection();
		    PreparedStatement stmt = null;
		    	ResultSet rs = null;

		    	Menu menu=new Menu();
	      try {
				
				stmt = conn.prepareStatement("select * from user where real_name='"+real_name+"'"); 		
				rs = stmt.executeQuery();
				if (rs.next()) { 	 
					menu=new Menu(rs.getInt("role_id"),rs.getString("real_name"),rs.getString("role_name"));
					list.add(menu);
				}else {
					menu=null;
				}
		
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JdbcUtils.close(conn, stmt, rs);
			}
		return null;
	}

	@Override
	public Menu find(String role_name) {
		// TODO Auto-generated method stub
		List<Menu> list =new ArrayList<Menu>();
		 Connection conn = JdbcUtils.getConnection();
		    PreparedStatement stmt = null;
		    	ResultSet rs = null;

		    	Menu menu=new Menu();
	      try {
				
				stmt = conn.prepareStatement("select * from role where role_name='"+role_name+"'"); 		
				rs = stmt.executeQuery();
				if (rs.next()) { 	 
					menu=new Menu(rs.getInt("role_id"),rs.getString("real_name"),rs.getString("role_name"));
					list.add(menu);
				}else {
					menu=null;
				}
		
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JdbcUtils.close(conn, stmt, rs);
			}
		return null;
	}

	@Override
	public List<Menu> findAll() {
		List<Menu> list =new ArrayList<Menu>();
		Connection conn = JdbcUtils.getConnection();
	    PreparedStatement stmt = null;
	    	ResultSet rs = null;
		    	try {
					
					stmt = conn.prepareStatement("SELECT user_login.user_id,user.real_name,role.role_name FROM "
							+ "user_login LEFT JOIN user ON user.user_id = user_login.user_id "
							+ "LEFT JOIN permission ON permission.user_id = user_login.user_id LEFT JOIN role ON role.role_id = permission.role_id"
							);
					rs = stmt.executeQuery();
					while (rs.next()) {
						System.out.println(rs.getInt("user_id"));
						System.out.println(rs.getString("real_name"));
						System.out.println(rs.getString("role_name"));
						Menu menu=new Menu(rs.getInt("user_id"),rs.getString("real_name"),rs.getString("role_name"));
	                    list.add(menu);
					}
			
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					JdbcUtils.close(conn, stmt, rs);
				}
			return list;
			
	}
	
       

	@Override
	public List<Menu> selectByCondition(String role_name) {
		// TODO Auto-generated method stub
		return null;
	}
}
